package com.tsinghuabigdata.edu.ddmath.module.ddwork.bean;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tsinghuabigdata.edu.ddmath.module.mylearn.bean.ImageCorrectBean;
import com.tsinghuabigdata.edu.ddmath.util.AppLog;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * 问题本地图片信息
 */
public class LocalQuestionInfo extends QuestionInfo implements Serializable {

    private static final long serialVersionUID = 8086359967898611335L;

    //答案图片
    private String localpath;       //图片保存在本地的路径

    //答案区域
    private QuestionRect questionRect;

    //批改区域
    private ArrayList<ImageCorrectBean> correctRects;

    //for 上传使用
    public JSONObject getJson(){
        JSONObject json = new JSONObject();
        try {
            json.put( "questionId", questionId );

            getQuestionRect();      //保证 questionRect 对象存在
            json.put( "x", questionRect.getX() );
            json.put( "y", questionRect.getY() );
            json.put( "width", questionRect.getWidth() );
            json.put( "height", questionRect.getHeight() );

        }catch (Exception e){
            AppLog.i( e.toString() );
        }
        return json;
    }

    //for 本地保存使用
    public JSONObject getJsonObject(){
        JSONObject json = new JSONObject();
        try {
            json.put( "questionId", questionId );
            json.put( "localpath", localpath );
            json.put( "questionRect", getQuestionRect().getJsonObject() );
        }catch (Exception e){
            AppLog.i( e.toString() );
        }
        return json;
    }

    //-------------------------------------------------------------------

    public String getLocalpath() {
        return localpath;
    }

    public void setLocalpath(String localpath) {
        this.localpath = localpath;
    }

    public String getQuestionId() {
        return questionId;
    }

    public void setQuestionId(String questionId) {
        this.questionId = questionId;
    }

    public QuestionRect getQuestionRect() {
        if( questionRect == null ){

            ArrayList<QuestionRect> list = new Gson().fromJson(questionArea, new TypeToken<ArrayList<QuestionRect>>() {
            }.getType());
            if( list.size() > 0 ) questionRect = list.get(0);           //默认获取第一个
        }
        return questionRect;
    }

    public void setQuestionRect(QuestionRect questionRect) {
        this.questionRect = questionRect;
    }

    public ArrayList<ImageCorrectBean> getCorrectRects() {
        if( correctRects == null ){
            correctRects = new Gson().fromJson(answerArea, new TypeToken<ArrayList<QuestionRect>>() {
            }.getType());
        }
        return correctRects;
    }

    public void setCorrectRects(ArrayList<ImageCorrectBean> correctRects) {
        this.correctRects = correctRects;
    }
}
